package com.bw.controller;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.bw.service.TmsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class TmsController {
    //注入业务层
    @Autowired
    TmsService tmsService;

    //    1.1统计物流运单相关总数，组件选择核心指标(当日可以按照数据里日期随意指定一个日期)
    //（指标：当日完成运输次数、当日完成运输里程、当日完成运输时长） sql
    @RequestMapping("/findWan")
    public Object findWan(String nowDate){
        List<Map> list = tmsService.findWan(nowDate);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",0);
        jsonObject.put("msg","");
        JSONArray dataArray = new JSONArray();

        JSONObject dimObject = new JSONObject();
        dimObject.put("dimValue","");
        JSONArray indArray = new JSONArray();
        JSONObject nameObject = new JSONObject();
        nameObject.put("name","当日完成运输次数");
        nameObject.put("value",list.get(0).get("当日完成运输次数"));
        indArray.add(nameObject);
        dimObject.put("indicators",indArray);
        dataArray.add(dimObject);

        JSONObject dimObject1 = new JSONObject();
        dimObject1.put("dimValue","");
        JSONArray indArray1 = new JSONArray();
        JSONObject nameObject1 = new JSONObject();
        nameObject1.put("name","当日完成运输里程");
        nameObject1.put("value",list.get(0).get("当日完成运输里程"));
        indArray1.add(nameObject1);
        dimObject1.put("indicators",indArray1);
        dataArray.add(dimObject1);

        JSONObject dimObject2 = new JSONObject();
        dimObject2.put("dimValue","");
        JSONArray indArray2 = new JSONArray();
        JSONObject nameObject2 = new JSONObject();
        nameObject2.put("name","当日完成运输时长");
        nameObject2.put("value",list.get(0).get("当日完成运输时长"));
        indArray2.add(nameObject2);
        dimObject2.put("indicators",indArray2);
        dataArray.add(dimObject2);

        jsonObject.put("data",dataArray);
        return jsonObject;
    }

    //统计当日物流下单次数 sql
    @RequestMapping("/findCount")
    public Object findCount(String nowDate){
        List<Map> list = tmsService.findCount(nowDate);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",0);
        jsonObject.put("msg","");
        jsonObject.put("data",list.get(0).get("cs"));
        return jsonObject;
    }

    //统计完运时长存储过程 sql
    @RequestMapping("/findTime")
    public Object findTime(String nowDate){
        List<Map> list = tmsService.findTime(nowDate);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",0);
        jsonObject.put("msg","");
        JSONObject dataObject = new JSONObject();
        JSONArray colArray = new JSONArray();

        JSONObject nameObject1 = new JSONObject();
        nameObject1.put("name","机构");
        nameObject1.put("id","jg");
        colArray.add(nameObject1);

        JSONObject nameObject2 = new JSONObject();
        nameObject2.put("name","完运里程");
        nameObject2.put("id","lc");
        colArray.add(nameObject2);

        JSONObject nameObject3 = new JSONObject();
        nameObject3.put("name","完运次数");
        nameObject3.put("id","count");
        colArray.add(nameObject3);

        JSONObject nameObject4 = new JSONObject();
        nameObject4.put("name","完运时长");
        nameObject4.put("id","time");
        colArray.add(nameObject4);

        JSONObject nameObject5 = new JSONObject();
        nameObject5.put("name","每次平均时长");
        nameObject5.put("id","avgTime");
        colArray.add(nameObject5);

        JSONObject nameObject6 = new JSONObject();
        nameObject6.put("name","每次平均里程");
        nameObject6.put("id","avgLc");
        colArray.add(nameObject6);
        dataObject.put("columns",colArray);

        JSONArray rowArray = new JSONArray();
        for (Map map : list) {
            JSONObject rowObject = new JSONObject();
            rowObject.put("jg",map.get("机构"));
            rowObject.put("lc",map.get("完运里程"));
            rowObject.put("count",map.get("完运次数"));
            rowObject.put("time",map.get("完运时长"));
            rowObject.put("avgTime",map.get("每次平均时长"));
            rowObject.put("avgLc",map.get("每次平均里程"));
            rowArray.add(rowObject);
        }

        dataObject.put("rows",rowArray);
        jsonObject.put("data",dataObject);
        return jsonObject;
    }

    //统计各转运站派送成功数 sql
    @RequestMapping("/findPai")
    public Object findPai(String nowDate){
        List<Map> list = tmsService.findPai(nowDate);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",0);
        jsonObject.put("msg","");
        JSONObject dataObject = new JSONObject();
        JSONArray catArray = new JSONArray();
        for (Map map : list) {
            catArray.add(map.get("name"));
        }
        dataObject.put("categories",catArray);

        JSONArray serArray = new JSONArray();
        JSONObject nameObject = new JSONObject();
        nameObject.put("name","name");

        JSONArray dataArray = new JSONArray();
        for (Map map : list) {
            dataArray.add(map.get("派送成功次数"));
        }
        serArray.add(nameObject);
        nameObject.put("data",dataArray);
        dataObject.put("series",serArray);
        jsonObject.put("data",dataObject);
        return jsonObject;
    }
}
